#include "Borrower_t.h"
#include "Book_t.h"
#include "LibraryException.h"

void Borrower_t::borrowBook(const Book_t * book) {
	borrowed_books_.push_back(book);
}

void Borrower_t::returnBook(const Book_t * book) {
	list<const Book_t *>::iterator it = borrowed_books_.begin();
	while (it!=borrowed_books_.end()) {
		if (*it == book)
			break;
		it++;
	}
	if (it == borrowed_books_.end()) //Didn't borrow this book
		throw CantReturnBookExeption();

	borrowed_books_.erase(it);
}


ostream &operator<< (ostream& os, const Borrower_t & borrower) {
	os << "ID: " << borrower.id_ << endl \
		<< "Name: " << borrower.name_ << endl;

	//borrowed books
	os << endl << "Borrowed books:" << endl;
	int i = 1;
	list<const Book_t *>::const_iterator it = borrower.borrowed_books_.begin();
	while (it!=borrower.borrowed_books_.end()) {
		os << i++ << ": "  << (*it)->getName() << ", " << (*it)->getAuthor() << ", " << (*it)->getIsbn() << endl;
		it++;
	}
	if (i == 1) os << "(none)" << endl;

	return os;

}